/**
 * 通过设置display属性来设置div对象是显示或是隐藏
 * @param	objId	元素的id名称
 * @param	status	显示：inline；隐藏：none
 */
function	showHideDiv(objId){
	var obj = document.getElementById(objId);
	if(!obj){
		alert("showDisplayDiv出错：对象不存在！");
		return	false;
	}else{
		if (obj.style.display=="") {
			obj.style.display="none"
			//switchPoint.innerText=4
			}
		else {
			obj.style.display=""
			//switchPoint.innerText=3
			}
	}
}
function MM_findObj(n, d) { // v4.01
	var p, i, x;
	if (!d)
		d = document;
	if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all)
		x = d.all[n];
	for (i = 0;!x && i < d.forms.length; i++)
		x = d.forms[i][n];
	for (i = 0;!x && d.layers && i < d.layers.length; i++)
		x = MM_findObj(n, d.layers[i].document);
	if (!x && d.getElementById)
		x = d.getElementById(n);
	return x;
}
function MM_swapImgRestore() { // v3.0
	var i, x, a = document.MM_sr;
	for (i = 0;a && i < a.length && (x = a[i]) && x.oSrc; i++)
		x.src = x.oSrc;
}
function MM_swapImage() { // v3.0
	var i, j = 0, x, a = MM_swapImage.arguments;
	document.MM_sr = new Array;
	for (i = 0;i < (a.length - 2); i += 3)
		if ((x = MM_findObj(a[i])) != null) {
			document.MM_sr[j++] = x;
			if (!x.oSrc)
				x.oSrc = x.src;
			x.src = a[i + 2];
		}
}
/*
* option对像
*/
function OptionObject(id, type, value) {
	this.id = id;//对应id
	this.type = type;//适用类型
	this.value = value;//实际值
}
/*添加option*/
function addOption(id, optionType, optionValue) {
	baseAjaxService.saveOption(optionType, optionValue, function (data) {
		var centerNode = $("#" + id);
		var ul = centerNode.next().next().prepend($("<li optionId='" + data.id + "' optionType='" + data.optionType + "'>" + data.optionValue + "</li>")).hide();
		ul.find("li:first").css({"background-color":"#3366cc", "color":"#ffffff"});
		centerNode.next().click();
	});
}
/*转移option*/
function transferOption(sourceId, targetId) {
	var sourceSelect = $("#" + sourceId);
	var targetSelect = $("#" + targetId);
	sourceSelect.find("option:selected").each(function (i, sourceOption) {
		var have = false;
		targetSelect.find("option").each(function (j, targetOption) {
			if (sourceOption.value == targetOption.value) {
				have = true;
				$(sourceOption).remove();
				return false;
			}
		});
		if (!have) {
			targetSelect.prepend(sourceOption);
		}
	});
}
/*获取元素的位置*/
function getposition(ob) {
	return [$(ob).offset().left, $(ob).offset().top];
}

// cookie操作
//读cookie
function readCookie(name) {
	var cookieValue = "";
	var search = name + "=";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1) {
				end = document.cookie.length;
			}
			cookieValue = unescape(document.cookie.substring(offset, end));
		}
	}
	return cookieValue;
}

// 写cookie
function writeCookie(name, value, hours) {
	var expire = "";
	if (hours != null) {
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	}
	document.cookie = name + "=" + escape(value) + expire;
}
/**
*创建右键菜单
*/
function createRightKeyMenu(ob, options, funcs) {
	ob.bind("contextmenu", function (event) {
		var rightKeyDiv;
		var mousedownOb = $(event.target);
		if ($("#rightKeyDiv").size() == 0) {
			rightKeyDiv = $("<div id='rightKeyDiv' class='xfy_tool_02'><ul><div class='qin'></div></ul></div>");
			$("body").append(rightKeyDiv);
		} else {
			rightKeyDiv = $("#rightKeyDiv");
		}
		rightKeyDiv.find("ul").prepend($(options));
		var x = event.pageX;
		var y = event.pageY;
		rightKeyDiv.show();
		rightKeyDiv.css({"top":y, "left":x});
		for (var funcName in funcs) {
			rightKeyDiv.find("#" + funcName).click(funcs[funcName]);
		}
		event.preventDefault();
	});
}

function closeByClickWindow(){
$("html").click(function () {
		$("span.mySelect > ul").hide();
		$("#rightKeyDiv").hide();
		$("#xfy_tool_02").hide();
		$("#rightKeyDiv").hide();
	});
}

/*创建自定义下拉框*/
function createMySelect() {
	
	//创建包裹input的span节点
	var parentNode = $("<span class='mySelect'></span>");
	//这是input节点，一切都以他为中心
	var centerNode = $("input[mytype=mySelect]");
	centerNode.addClass("xfy_text_before");
	centerNode.attr("autocomplete", "off");
	//创建下拉选项的父节点
	var optionNode = $("<ul></ul>").hide();
	//把input节点包在span里面
	centerNode.wrap(parentNode);
	//在input节点后跟一个a
	centerNode.after("<a href='javascript:void(0)'></a>");
	
	//在a后跟下拉选项的父节点(ul)
	centerNode.next().after(optionNode);
	//获得所有控件元素的数组
	var mySelectArray = centerNode.get();
	$(mySelectArray).each(function (i, mySelect) {
		var optionValueArray = eval($(this).attr("optionarray"));
		for (var j = 0; j < optionValueArray.length; j++) {
			if (typeof (optionValueArray[j]) == "string") {
				$(this).next().next().append($("<li>" + optionValueArray[j] + "</li>"));
			} else {
				$(this).next().next().append($("<li optionId='" + optionValueArray[j].id + "' optionType='" + optionValueArray[j].type + "'>" + optionValueArray[j].value + "</li>"));
				if ($("#" + $(mySelect).attr("id") + "Value").val() == optionValueArray[j].id) {
					$(mySelect).val(optionValueArray[j].value);
				}
			}
		}
	});
	centerNode.keyup(function (event) {
		var ul = $(event.target).next().next();
		var positionArray = getposition(event.target);
		var left = positionArray[0] + "px";
		var top = 18 + positionArray[1] + "px";
		var inputWidth = $(event.target).width() + 23;
		ul.find("li").width(inputWidth - 1);
	
	//隐藏不匹配的li
		$(event.target).next().next().children().each(function (i, liNode) {
			if ($(liNode).html().indexOf($(event.target).val()) != -1) {
				$(liNode).show();
			} else {
				$(liNode).hide();
			}
		});
	//当高为200时，将其还原到真实宽度
		if (ul.height() == 200) {
			ul.css("height", "auto");
		}
		if (ul.height() >= 200) {
			ul.css("height", "200px");
			ul.width(inputWidth + 16);
		} else {
			ul.width(inputWidth);
		}
		ul.css("left", left);
		ul.css("top", top);
		ul.show();
	}).blur(function (event) {
		var isFound = false;
		$(event.target).next().next().children().each(function (i, liNode) {
			if ($(liNode).html() == $(event.target).val()) {
				isFound = true;
			}
		});
		if (!isFound) {
			$(event.target).val("");
		}
	});
	centerNode.next().click(function (event) {
		var ul = $(this).next();
		var positionArray = getposition(event.target.previousSibling);
		var left = positionArray[0] + "px";
		var top = 18 + positionArray[1] + "px";
		var inputWidth = $(this.previousSibling).width() + 23;
		ul.find("li").width(inputWidth - 1);
		ul.toggle();
		ul.css("left", left);
		ul.css("top", top);
		ul.children().show();
	//关闭除了本身之外的其他同类ul
		$("span.mySelect > ul").not(ul).hide();
		if (ul.height() == 200) {
			ul.css("height", "auto");
		}
		if (ul.height() >= 200) {
			ul.css("height", "200px");
			ul.width(inputWidth + 16);
		} else {
			ul.width(inputWidth);
		}
		event.stopPropagation();
	});
	$("span.mySelect > ul >li").live("mouseover", function () {
		$(this).css({"background-color":"#3366cc", "color":"#ffffff"});
	}).live("mouseout", function () {
		$(this).css({"background-color":"#f5f5f5", "color":"#000000"});
	}).live("click", function (event) {
		var inputNode = $(event.target).parent().prev().prev();
		inputNode.focus().val($(event.target).html());
		//将实际值赋给一个hidden域
		$("#" + inputNode.attr("id") + "Value").val($(event.target).attr("optionId"));
	}).live("contextmenu", function (event) {
		var mousedownOb = $(event.target);
		//当页面配置本下拉列表不可编辑时，屏蔽右键
		if (centerNode.attr("edit") != "false") {
			var x = event.pageX;
			var y = event.pageY;
			var rightKeyDiv=$("#rightKeyDiv");
			if(rightKeyDiv.size()==0){
			rightKeyDiv = $("<div id='rightKeyDiv' class='xfy_tool_02'><ul><li><a href='javascript:;'>\u5220\u9664\u672c\u6761\u76ee</a></li><div class='qin'></div></ul></div>");
			$("body").append(rightKeyDiv);
			}
			rightKeyDiv.show();
			rightKeyDiv.css({"top":y, "left":x});
			rightKeyDiv.find("a").click(function () {
				baseAjaxService.deleteOption(mousedownOb.attr("optionId"));
				mousedownOb.css("position", "relative").animate({opacity:"hide", top:$(window).height() - $(this).height() - $(this).position().top}, 777, function () {
					mousedownOb.hide().remove();
				});
				rightKeyDiv.hide();
				event.stopPropagation();
			});
			event.preventDefault();
		}
	});
	//完成
}
/*
*创建浮动层
*/
function createFloatFloor(id) {
	var centerFloor = $("#" + id);
	var backgroundFloor = $("#backgroundFloor");
//透明层
	backgroundFloor.css({"opacity":"0.7", "height":w_h}).hide();
	var contentTd = centerFloor.find("td.xfy_content");
	var i = contentTd.width();
	if (i < 360) {
		contentTd.css("width", "360px");
	}

//居中透明层
	var w_w = $("body").width();
	var w_h = $("body").height();
	var x_w = centerFloor.width();
	var x_h = centerFloor.height();
	var l = (w_w - x_w) / 2;
	var t = (w_h - x_h) / 2;
	centerFloor.css("left", l);
	centerFloor.css("top", t);
	backgroundFloor.css("height", w_h);
	backgroundFloor.fadeIn(777);
	centerFloor.fadeIn(777);
	centerFloor.find(".xfy_fc002 > a").click(function () {
		backgroundFloor.hide();
		centerFloor.hide();
	}).end().find(":button").click(function () {
		backgroundFloor.fadeOut(555);
		centerFloor.fadeOut(555);
	});
}

//去掉点击后的虚线
function clearBrokenLine() {
	$("a").focus(function (event) {
		event.target.blur();
	});
	$(":button").focus(function (event) {
		event.target.blur();
	});
	$(":submit").focus(function (event) {
		event.target.blur();
	});
	$(":reset").focus(function (event) {
		event.target.blur();
	});
}
/*
*消除ie6bug
*/
function clearBug() {
	/* 无聊的li 和 可恶的IE6 */
	$(".xfy_form > ul > li").css({"display":"block", "float":"left", "clear":"left", "padding-top":"5px", "padding-bottom":"5px"});
}
/*创建表单样式*/
function createFormStyle() {
	$(":text,:password").focus(function () {
		$(this).removeClass().addClass("required xfy_text_after");
		/*value 效果*/
	}).blur(function () {
		$(this).removeClass().addClass("required xfy_text_before");
		/*value 效果*/
		if ($(this).parents("form").attr("isupdate") != "true") {
			if (this.value == "") {
				this.value = this.defaultValue;
				$(this).css("color", "#777777");
			}
		}
		if (this.value != this.defaultValue) {
			$(this).removeClass().addClass("xfy_text_after");
		}
	});
	$("textarea").click(function () {
		$(this).removeClass().addClass("xfy_textarea_after");
	}).blur(function () {
		$(this).removeClass().addClass("xfy_textarea_before");
	});
	$("fieldset").find("li").mouseover(function () {
		$(this).css("background-color", "#f9f9f9");
	}).mouseout(function () {
		$(this).css("background-color", "#ffffff");
	});
	$(".select_m > option:even").css("background-color", "#f2f2f2");
}
// 绑定等待的小羊
function sheepWaiting() {
	$(window).scroll(function () {
		var i = $(document).scrollTop();
		$("#sheep_loading").css("top", i);
	});
	$("[linkOut=true]").click(function () {
		$("#sheep_loading").css("display", "block");
	});
}

// 设置按钮样式
function buttonStyle() {
	$(".xfy_button_before").mouseover(function () {
		$(this).removeClass().addClass("xfy_button_after");
	}).mouseout(function () {
		$(this).removeClass().addClass("xfy_button_before");
	});
}

// 设置用户个人喜好
function createFavorButton() {
	$("#top_tool").click(function (event) {
		event.stopPropagation();
		var open = document.getElementById("xfy_tool_02");
		var closeArray = document.getElementsByTagName("div");
		var positionArray = getposition(this);
		open.style.display = "block";
		open.style.left = -60 + positionArray[0] + "px";
		open.style.top = 20 + positionArray[1] + "px";
		for (var i = 0; i < closeArray.length; i++) {
			if (closeArray[i].className == "oc_floor") {
				if (closeArray[i].id != floorName) {
					closeArray[i].style.display = "none";
				}
			}
		}
	});
}
function baseReady() {
	clearBrokenLine();
	sheepWaiting();
	createFormStyle();
	clearBug();
	createMySelect();
	buttonStyle();
	createFavorButton();
	closeByClickWindow();
	$("#sheep_loading").fadeOut("slow");
}

